package com.swu.pad.activity.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.swu.pad.activity.entity.Activity;
import com.swu.pad.activity.entity.ActivityGroupVision;
import com.swu.pad.activity.service.ActivityService;
import com.swu.pad.system.dto.ResultDto;
import com.swu.pad.system.entity.User;

@Controller
@RequestMapping(value = "/activity")
public class ActivityController {

	@Autowired
	private ActivityService activityService;

	@ResponseBody
	@RequestMapping(value = "/createActivity")
	public String createActivity(Activity activity) {
		activityService.createActivity(activity);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/getActivities")
	public List<Activity> getActivities(String assignmentId) {
		return activityService.getActivityByAssignmentId(assignmentId);
	}

	@ResponseBody
	@RequestMapping(value = "/deleteActivity")
	public String deleteActivity(String activityId) {
		activityService.deleteActivity(activityId);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/getActivityGroupVisions")
	public List<ActivityGroupVision> getActivityGroupVisions(String activityId) {
		return activityService.getActivityGroupVisions(activityId);
	}

	@ResponseBody
	@RequestMapping(value = "/getActivityGroupVision")
	public ResultDto getActivityGroupVision(String activityId, HttpSession session) {
		User user = (User) session.getAttribute("user");
		ActivityGroupVision activityGroupVision = activityService.getActivityGroupVision(activityId, user.getId());
		ResultDto result = new ResultDto();
		result.setCode(ResultDto.CODE_OK);
		result.setMessage("ok");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("vision", activityGroupVision);
		result.setData(map);
		//System.out.println(result.toString());
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/saveActivityGroupVision")
	public String saveActivityGroupVision(ActivityGroupVision activityGroupVision) {
		activityService.saveActivityGroupVision(activityGroupVision);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/getAssignmentCurrentActivity")
	public Activity getAssignmentCurrentActivity(String assignmentId) {
		return activityService.getAssignmentCurrentActivity(assignmentId);
	}

	@ResponseBody
	@RequestMapping(value = "/getAllCurrentActivity")
	public List<Map<String, Object>> getAllCurrentActivity(HttpSession session) {
		User user = (User) session.getAttribute("user");
		if (User.ROLE_STUDENT.equals(user.getRole())) {
			return activityService.getAllCurrentActivity(user.getId());
		}
		return null;
	}
	
	//获得 学生可以进行的 活动   在当前活动之前的活动
	@ResponseBody
	@RequestMapping(value = "/getCanDoActivities")
	public List<Activity> getCanDoActivities(HttpSession session,String assignmentId) {
		User user = (User) session.getAttribute("user");
		List<Activity> actLists=activityService.getCanDoActivities(user,assignmentId);
			 return actLists;  
	}

}
